package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @CacheEvict(value = "setmealCache" ,key = "#dishDTO.categoryId")
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> insertDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}" + dishDTO);
        dishService.insertDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> searchDishes(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.searchDishes(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @CacheEvict(value = "DishCache" ,allEntries = true)
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result deleteDishes(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.deleteDishes(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> SearchDishById(@PathVariable Long id){
        log.info("根据id查询菜品：",id);
        DishVO dishVO = dishService.searchDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @CacheEvict(value = "DishCache" ,key = "#dishDTO.categoryId")
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(value = "DishCache" ,allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result updateDishStatus(@PathVariable Integer status ,Long id){
        log.info("修改菜品：{}", status,id);
        dishService.updateDishStatus(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> searchDishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品",categoryId);
        List<DishVO> dishes = dishService.searchDishByCategoryId(categoryId);
        return Result.success(dishes);

    }

}
